
public class Privileges {
	
	public int Principale;

	Conection Con=new Conection();
	Privileges()
	{
		
	}
		
	/*
	 * this function take two parameters : role, privilege .
	 * return true if the role has the privileges else false 
	 */
	public boolean HasPrivilege(String role, String priv){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=Con.PriviList(role); // it all the privileges that are assigned to a role
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals(priv))
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			Con.resultSet.close();
			Con.statement.close();
			Con.connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error from the HasPrivilege in privileges class");
		}
	}
	return(resulte);
	}
	/*
	 * this function checks accessing privileges for personal's personal information
	 */
	public boolean OwnPII(String subject){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=Con.OwnPII(subject);
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "own" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
			try {
				Con.resultSet.close();
				Con.statement.close();
				Con.connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Error from the OwnPII in privileges class");
			}
	}
	return(resulte);
	}
	
	public boolean IsProfessor(String subject,String course){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=Con.OwnPII(subject); // it all the privileges that are assigned to a role
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "own" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			Con.resultSet.close();
			Con.statement.close();
			Con.connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error from the IsProfessor in privileges class");
		}
	}
	return(resulte);
	}
	
	public boolean IsInCourse(String subject,String Object){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=Con.PIIPriv(subject,Object); 
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "ViewLimitedPII" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			Con.resultSet.close();
			Con.statement.close();
			Con.connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error from the IsIncourse in privileges class");
		}
	}
	return(resulte);
	}
	
}
